<%@ page contentType="text/html; charset=utf-8" %>
<%@ page import="java.util.List"%>
<%@ page import="java.util.Iterator"%>
<%@ page import="com.knowledge.DAO.KnowledgeDAO.DataSource"%>

<% String path = request.getContextPath(); %>
<%
	//TODO  得用户id
	String currId = (String) request.getParameter("userId");
	if(currId != null) {
		request.setAttribute("userId", currId);
		
	}
	if (currId == null) {
		currId = (String) request.getAttribute("userId");
	} 
	
	Integer currPage;
	Integer pageSize = 20;

	try {
		currPage = Integer.parseInt(request.getParameterValues("page")[0]);
	} catch (Exception e) {
		currPage = new Integer(1);
	}
	
	
	Integer totalRecord = Integer.parseInt(DataSource.queryApplyCount(currId));
	Integer pages = totalRecord / pageSize;
	if (totalRecord % pageSize != 0) {
		++pages;
	}
	
	if (currPage < 1) {
		currPage = 1;
	}
	
	if (currPage > pages) {
		if (pages == 0) {
			currPage = 1;
		} else {
			currPage = pages;
		}
	}

	Integer prePage = currPage - 1;
	Integer nextPage = currPage + 1;
	
	if (prePage < 1) {
		prePage = 1;
	}
	
	if (nextPage > pages) {
		if (pages == 0) {
			nextPage = 1;
		} else {
			nextPage = pages;
		}
	}
	
	List<String> list = DataSource.queryApply(currId, pageSize, currPage);
	Iterator<String> iter = null;
	if (list != null) {
		iter = list.iterator();
	}
%>
<div id="myFriendRequestsPanel" class="titledPanel width80 pcenter">
	<div class="titlediv">
    	<span>共有<%= totalRecord %>个好友邀请</span>
    </div>
    <div class="boarddiv">
<%	if (iter != null) { %>
<%		while(iter.hasNext()) { %>
<%			String uid = iter.next(); %>
<%         String name = DataSource.queryUserName(uid); %>
<%           request.setAttribute("friendId", uid); %>
<%  //TODO  插入头像 %> 
    	<div class="bordered width99">
            <span class="fltlt">
            	<img src='<%="avatar/avatar_"+ uid + ".jpg"%>'  class="picMax"/>
            </span>
            <span>
                <p><a href="profile.action?userId=<%=uid %>&loginId=<%=currId %>"><h5><%= name %></h5></a></p>
                <p class="pright"><a href=<%="agreerequest.action?userId=" + currId + "&friendId=" + uid %>>同意邀请</a></p><br /><br />
                <p class="pright"><a href=<%="disagreerequest.action?userId=" + currId + "&friendId=" + uid %>>拒绝邀请</a></p>
            </span>
            <div class="clearfloat" />
        </div>
<%		} %>
<%	} %>
    </div>
    <hr />
    <div class="containerPanel width0">
    	<div class="ltsidebar tleft"><a href="requests.jsp?page=<%= prePage %>">上一页</a></div>
    	<div class="rtsidebar tright"><a href="requests.jsp?page=<%= nextPage %>">下一页</a></div>
    	<div class="maincontent tcenter">第<%= currPage %>页，共<%= pages == 0 ? 1 : pages %>页</div>
    </div>
    <div class="clearfloat" />
</div>